from app import create_app,celery,db
import os
import sys
import logging

sys.path.append(os.getcwd())
logger = logging.getLogger(__name__)

config_name = os.getenv('CELERY_CONFIG') or 'development'
celery_app = create_app(config_name)
celery_app.app_context().push()

from celery.signals import task_retry,task_prerun,task_failure,task_internal_error

@task_prerun.connect
def task_prerun_handler(sender=None, headers=None, body=None, **kwargs):
    logger.info("task_prerun_handle")
    db.session.remove()

@task_retry.connect
def retry_handler(sender=None, headers=None, body=None, **kwargs):
    logger.info("retry_handle")
    db.session.remove()

@task_failure.connect
def task_failure_handler(sender=None, headers=None, body=None, **kwargs):
    logger.info("task_failure_handle")
    db.session.remove()

@task_internal_error.connect
def task_internal_error_handler(sender=None, headers=None, body=None, **kwargs):
    logger.info("task_internal_error_handle")
    db.session.remove()


from task.test import *